local _, private = ...

-- [[ Lua Globals ]]
local pairs = _G.pairs

-- [[ Core ]]
local F, C = _G.unpack(private.Aurora)

function private.FrameXML.StaticPopup()
    local r, g, b = C.r, C.g, C.b

    local function colourMinimize(f)
        if f:IsEnabled() then
            f.minimize:SetVertexColor(r, g, b)
        end
    end

    local function clearMinimize(f)
        f.minimize:SetVertexColor(1, 1, 1)
    end

    for i = 1, 4 do
        local frame = _G["StaticPopup"..i]
        local bu = _G["StaticPopup"..i.."ItemFrame"]
        local close = _G["StaticPopup"..i.."CloseButton"]

        _G["StaticPopup"..i.."ItemFrameNameFrame"]:Hide()
        _G["StaticPopup"..i.."ItemFrameIconTexture"]:SetTexCoord(.08, .92, .08, .92)

        bu:SetNormalTexture("")
        bu:SetHighlightTexture("")
        bu:SetPushedTexture("")
        F.CreateBG(bu)

        F.CreateBD(frame)

        for j = 1, 3 do
            F.Reskin(frame["button"..j])
        end

        F.ReskinClose(close)

        close.minimize = close:CreateTexture(nil, "OVERLAY")
        close.minimize:SetSize(9, 1)
        close.minimize:SetPoint("CENTER")
        close.minimize:SetTexture(C.media.backdrop)
        close.minimize:SetVertexColor(1, 1, 1)
        close:HookScript("OnEnter", colourMinimize)
        close:HookScript("OnLeave", clearMinimize)

        F.ReskinInput(_G["StaticPopup"..i.."EditBox"], 20)
        F.ReskinMoneyInput(_G["StaticPopup"..i.."MoneyInputFrame"])
    end

    _G.hooksecurefunc("StaticPopup_Show", function(which, text_arg1, text_arg2, data)
        local info = _G.StaticPopupDialogs[which]

        if not info then return end

        local dialog = _G.StaticPopup_FindVisible(which, data)
        if not dialog then
            local index = 1
            if info.preferredIndex then
                index = info.preferredIndex
            end
            for i = index, _G.STATICPOPUP_NUMDIALOGS do
                local frame = _G["StaticPopup"..i]
                if not frame:IsShown() then
                    dialog = frame
                    break
                end
            end

            if not dialog and info.preferredIndex then
                for i = 1, info.preferredIndex do
                    local frame = _G["StaticPopup"..i]
                    if not frame:IsShown() then
                        dialog = frame
                        break
                    end
                end
            end
        end

        if not dialog then return end

        if info.closeButton then
            local closeButton = _G[dialog:GetName().."CloseButton"]

            closeButton:SetNormalTexture("")
            closeButton:SetPushedTexture("")

            if info.closeButtonIsHide then
                for _, pixel in pairs(closeButton.pixels) do
                    pixel:Hide()
                end
                closeButton.minimize:Show()
            else
                for _, pixel in pairs(closeButton.pixels) do
                    pixel:Show()
                end
                closeButton.minimize:Hide()
            end
        end
    end)
end
